package com.difsonapps.android.marker.adapters;
import java.util.ArrayList;
import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Album;
import com.difsonapps.android.marker.beans.Photo;
import com.difsonapps.android.marker.beans.Video;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PhotosGridAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
	private List<Photo> data = new ArrayList<Photo>();
	private List<Video> videos = new ArrayList<Video>();
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private boolean modePhotos = true;
    int screenWidth;
    int colBasWidth = 100;
    int newWidth = 100;
 
	public PhotosGridAdapter(Activity activity, List<Photo> photos) {
        this.activity = activity;
		this.data = photos;
        Display display = activity.getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth();
        
        int nbCols = screenWidth/colBasWidth;
        if(nbCols*colBasWidth>screenWidth) nbCols = nbCols - 1;
        newWidth = screenWidth/nbCols;
        if(newWidth*nbCols>screenWidth) newWidth--;
        newWidth = newWidth - 1;
        Log.d("Numbers of cols", ""+nbCols);
        Log.d("New width", ""+newWidth);
	}
    
    public int getNewWidth(){
    	return newWidth;
    }
 
	public PhotosGridAdapter(Activity activity, List data, boolean modePhotos) {
        this.activity = activity;
        this.modePhotos = modePhotos;
        if(modePhotos) this.data = data;
		this.videos = data;
	}
 

    public boolean isModePhotos() {
		return modePhotos;
	}
	public void setModePhotos(boolean modePhotos) {
		this.modePhotos = modePhotos;
	}
	
    @Override
    public int getCount() {
        return isModePhotos()?data.size():videos.size();
    }
 
    @Override
    public Object getItem(int location) {
        return isModePhotos()?data.get(location):videos.get(location);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder = null;
 
		if (row == null) {
			inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.photos_item, null);
			holder = new ViewHolder();
			holder.imageTitle = (TextView) row.findViewById(R.id.text);
			holder.image = (NetworkImageView) row.findViewById(R.id.image);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.image.getLayoutParams();
	        params.width = newWidth;
	        params.height = newWidth;
	        holder.image.setLayoutParams(params);
			holder.play = (ImageView) row.findViewById(R.id.play);
			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}
		if(modePhotos){
			Photo item = data.get(position);
			holder.imageTitle.setText(item.getTitle());
			holder.image.setImageUrl(item.getImage(), imageLoader);
			holder.play.setVisibility(View.GONE);
		}else{
			Video item = videos.get(position);
			holder.imageTitle.setText(item.getName());
			holder.image.setImageUrl(item.getUrlThumbnail(), imageLoader);
			holder.play.setVisibility(View.VISIBLE);
		}
		return row;
	}
 
	static class ViewHolder {
		TextView imageTitle;
		NetworkImageView image;
		ImageView play;
	}

}
